package cn.bs.ktv;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ToastUtils;

import cn.bs.ktv.connection.ConnectionMgr;
import cn.bs.ktv.view.ConnectActivity;
import cn.bs.ktv.view.SingerListActivity;

public class MainActivity extends AppCompatActivity {
    private static final String logName = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        setContentView(R.layout.activity_main);

        checkConnectStatus();
        registerClickEvent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        checkConnectStatus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        ConnectionMgr.disconnect();
    }

    private void registerClickEvent() {
        Button btn_connect = findViewById(R.id.btn_connect);
        btn_connect.setOnClickListener(view -> {
            boolean connectFlag = ConnectionMgr.isConnected();
            if (connectFlag) {
                ToastUtils.showLong(R.string.has_connection_server);
            } else {
                Intent intent = new Intent(getBaseContext(), ConnectActivity.class);
                startActivity(intent);
            }
        });

        Button btn_singer = findViewById(R.id.btn_singer);
        btn_singer.setOnClickListener(view -> {
            boolean connectFlag = ConnectionMgr.isConnected();
            if (connectFlag) {
                Intent intent = new Intent(getBaseContext(), SingerListActivity.class);
                startActivity(intent);
            } else {
                ToastUtils.showLong(R.string.none_connection_server);
            }
        });
    }

    private void checkConnectStatus() {
        boolean connectFlag = ConnectionMgr.isConnected();
        String toolTipText = getText(connectFlag ? R.string.tip_has_connection : R.string.tip_none_connection).toString();

        Button btn_now_wifi = findViewById(R.id.btn_no_wifi);
        btn_now_wifi.setEnabled(connectFlag);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            btn_now_wifi.setTooltipText(toolTipText);
        }
    }
}